﻿using System;
using System.Drawing;
using System.Windows.Forms;
using StockUtil;

// [Browsable(false)]      //在属性编辑器中是否可见
// [ReadOnly(true)]       //该属性是否只读
// [DesignOnly(false)]     //该属性是否只能在设计器中修改
// [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]   //在设计器中修改后，该属性是否保存

namespace StockControl
{
    public partial class StockCaptionControl : ToolStrip
    {
        private Form _form = null;

        public StockCaptionControl()
        {
            InitializeComponent();

            ToolStripItem item = new ToolStripControlHost(pictureSys);
            item.Alignment = ToolStripItemAlignment.Left;
            item.AutoSize = false;
            item.Margin = new Padding(3, 0, 3, 0);
            this.Items.Add(item);

            item = new ToolStripControlHost(pictureClose);
            item.Alignment = ToolStripItemAlignment.Right;
            item.AutoSize = false;
            item.Margin = new Padding(0, 0, 3, 0);
            this.Items.Add(item);

            item = new ToolStripControlHost(pictureMaxRestore);
            item.Alignment = ToolStripItemAlignment.Right;
            item.AutoSize = false;
            item.Margin = new Padding(0, 0, 1, 0);
            this.Items.Add(item);

            item = new ToolStripControlHost(pictureMinimize);
            item.Alignment = ToolStripItemAlignment.Right;
            item.AutoSize = false;
            item.Margin = new Padding(0, 0, 1, 0);
            this.Items.Add(item);

            this.GripStyle = ToolStripGripStyle.Hidden;
        }

        void OwnerForm_Resize(object sender, EventArgs e)
        {
            ChangeMaximizeIcon();
        }

        private void pictureSys_Click(object sender, EventArgs e)
        {
            Point vPoint = this.Parent.PointToScreen(new Point(0, Height));
            Rectangle vRect;
            Win32Helper.SendMessage(_form.Handle, Win32Helper.WM_SYSCOMMAND, Win32Helper.TrackPopupMenu(Win32Helper.GetSystemMenu(_form.Handle, false), Win32Helper.TPM_RETURNCMD | Win32Helper.TPM_LEFTBUTTON, vPoint.X, vPoint.Y, 0, _form.Handle, out vRect), 0);
        }

        private void pictureMinimize_Click(object sender, EventArgs e)
        {
            _form.WindowState = FormWindowState.Minimized;
        }

        private void pictureMaximize_Click(object sender, EventArgs e)
        {
            if (_form.WindowState == FormWindowState.Normal)
            {
                _form.WindowState = FormWindowState.Maximized;
            }
            else if (_form.WindowState == FormWindowState.Maximized)
            {
                _form.WindowState = FormWindowState.Normal;
            }
        }

        private void pictureClose_Click(object sender, EventArgs e)
        {
            _form.Close();
        }

        private void CaptionBar_MouseDown(object sender, MouseEventArgs e)
        {
            //拖动菜单栏，移动窗口            
            if (_form.WindowState != FormWindowState.Maximized)
            {
                Win32Helper.ReleaseCapture();
                Win32Helper.SendMessage(_form.Handle, Win32Helper.WM_SYSCOMMAND, Win32Helper.SC_MOVE + Win32Helper.HTCAPTION, 0);
            }

            //双击菜单栏，更改窗口状态
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                if (_form.WindowState == FormWindowState.Normal)
                    _form.WindowState = FormWindowState.Maximized;
                else if (_form.WindowState == FormWindowState.Maximized)
                    _form.WindowState = FormWindowState.Normal;
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            _form = this.FindForm();
            if (_form == null)
            {
                throw new Exception("必须放置在窗体中");
            }

            _form.Resize += new EventHandler(OwnerForm_Resize);

            base.OnPaint(pe);
        }

        private void ChangeMaximizeIcon()
        {
            if (_form.WindowState == FormWindowState.Maximized)
            {
                pictureMaxRestore.Image = pictureRestore.Image;
            }
            else if (_form.WindowState == FormWindowState.Normal)
            {
                pictureMaxRestore.Image = pictureMaximize.Image;
            }
        }
    }
}
